﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Barbar.UrlGuard.Controls {
  public class ChainTreeToolStrip : ToolStrip, ILocalizable {
    private ToolStripItem m_AddChain;
    private ToolStripItem m_EditChain;
    private ToolStripItem m_RemoveChain;
    private ToolStripItem m_AddUrl;
    private ToolStripItem m_EditUrl;
    private ToolStripItem m_RemoveUrl;

    public event EventHandler<ChainTreeToolStripEventArgs> Command;

    public ChainTreeToolStrip() {
      m_AddChain = new ToolStripButton("Add new chain");
      m_AddChain.Tag = ChainTreeToolStripCommand.AddChain;
      m_AddChain.Click += new EventHandler(OnMenuClick);

      m_EditChain = new ToolStripButton("Edit chain");
      m_EditChain.Tag = ChainContextMenuCommand.EditChain;
      m_EditChain.Click += new EventHandler(OnMenuClick);

      m_RemoveChain = new ToolStripButton("Remove chain");
      m_RemoveChain.Click += new EventHandler(OnMenuClick);
      m_RemoveChain.Tag = ChainTreeToolStripCommand.RemoveChain;

      m_AddUrl = new ToolStripButton("Add new url");
      m_AddUrl.Click += new EventHandler(OnMenuClick);
      m_AddUrl.Tag = ChainTreeToolStripCommand.AddUrl;

      m_EditUrl = new ToolStripButton("Edit url");
      m_EditUrl.Click +=new EventHandler(OnMenuClick);
      m_EditUrl.Tag = ChainTreeToolStripCommand.EditUrl;

      m_RemoveUrl = new ToolStripButton("Remove url");
      m_RemoveUrl.Click += new EventHandler(OnMenuClick);
      m_RemoveUrl.Tag = ChainTreeToolStripCommand.RemoveUrl;

      Items.AddRange(new ToolStripItem[] {
        m_AddChain, m_EditChain, m_RemoveChain, m_AddUrl, m_EditUrl, m_RemoveUrl
      });
    }

    private void OnMenuClick(object sender, EventArgs e) {
      ToolStripButton control = (ToolStripButton)sender;
      ChainTreeToolStripCommand command = (ChainTreeToolStripCommand)control.Tag;

      if (Command != null) {
        Command(this, new ChainTreeToolStripEventArgs(command));
      }
    }

    public void UpdateToolStrip(ChainTreeView treeView) {
      if (treeView == null)
        throw new ArgumentNullException("treeView");

      if (treeView.SelectedEntry != null) {
        m_AddChain.Enabled = true;
        m_RemoveChain.Enabled = false;
        m_EditChain.Enabled = false;
        m_AddUrl.Enabled = true;
        m_EditUrl.Enabled = true;
        m_RemoveUrl.Enabled = true;
        return;
      }

      if (treeView.SelectedChain != null) {
        m_AddChain.Enabled = true;
        m_RemoveChain.Enabled = true;
        m_EditChain.Enabled = true;
        m_AddUrl.Enabled = true;
        m_EditUrl.Enabled = false;
        m_RemoveUrl.Enabled = false;
        return;
      }

      m_AddChain.Enabled = true;
      m_RemoveChain.Enabled = false;
      m_EditChain.Enabled = false;
      m_AddUrl.Enabled = false;
      m_EditUrl.Enabled = false;
      m_RemoveUrl.Enabled = false;
    }

    #region ILocalizable Members

    public void Localize(ILocalizator localizator) {
      if (localizator == null)
        throw new ArgumentNullException("localizator");

      m_AddChain.Text = localizator.Translate("TreeCommand_AddChain");
      m_EditChain.Text = localizator.Translate("TreeCommand_EditChain");
      m_RemoveChain.Text = localizator.Translate("TreeCommand_RemoveChain");
      m_AddUrl.Text = localizator.Translate("TreeCommand_AddUrl");
      m_EditUrl.Text = localizator.Translate("TreeCommand_EditUrl");
      m_RemoveUrl.Text = localizator.Translate("TreeCommand_RemoveUrl");
    }

    #endregion
  }
}
